<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>ZGENERATION</title>
    <link rel="icon" href="./static/logo-z.png">
    <script src='./static/mui.min.js'></script>
    <script src='./static/OpenApp.js'></script>
  </head>
  <body>
    <div id="app"></div>
    <div id="lottieFixed" style="background: #111214;color:#fff; width: 100%;min-height: 100vh;">
      <iframe style="width: 60%;height: 100vh;margin-left: 20%;" src="./static/lottie/demo.html" frameborder="0"></iframe>
    </div>
    <!-- built files will be auto injected -->
  </body>
  <script>
    document.getElementById('lottieFixed').style.display = 'block';
    document.getElementById('app').style.display = 'none';
    setTimeout(()=>{
      document.getElementById('lottieFixed').style.display = 'none';
      document.getElementById('app').style.display = 'block';
    },7000) 
    //检测版本
    var wgtVer=null;  
    function plusReady(){  
      // ......  
      // 获取本地应用资源版本号  
      plus.runtime.getProperty(plus.runtime.appid,function(inf){  
          wgtVer=inf.version;  
      });  
    }  
    if(window.plus){  
    plusReady();  
    }else{  
    document.addEventListener('plusready',plusReady,false);  
    }

    // 检测更新  
    var checkUrl="http://demo.dcloud.net.cn/test/update/check.php";  
    function checkUpdate(){  
    plus.nativeUI.showWaiting("检测更新...");  
    var xhr=new XMLHttpRequest();  
    xhr.onreadystatechange=function(){  
        switch(xhr.readyState){  
            case 4:  
            plus.nativeUI.closeWaiting();  
            if(xhr.status==200){  
                var newVer=xhr.responseText;  
                if(wgtVer&&newVer&&(wgtVer!=newVer)){  
                    downWgt();  // 下载升级包  
                }else{  
                    plus.nativeUI.alert("无新版本可更新！");  
                }  
            }else{  
                plus.nativeUI.alert("检测更新失败！");  
            }  
            break;  
            default:  
            break;  
        }  
    }  
    xhr.open('GET',checkUrl);  
    xhr.send();  
    }

    // 下载wgt文件  
    var wgtUrl="https://script-fileoss.oss-cn-beijing.aliyuncs.com/apk/com.z.generation.wgt";  
    function downWgt(){  
    plus.nativeUI.showWaiting("下载wgt文件...");  
    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){  
        if ( status == 200 ) {   
            installWgt(d.filename); // 安装wgt包  
        } else {  
            plus.nativeUI.alert("下载wgt失败！");  
        }  
        plus.nativeUI.closeWaiting();  
    }).start();  
    }

    // 更新应用资源  
    function installWgt(path){  
    plus.nativeUI.showWaiting("安装wgt文件...");  
    plus.runtime.install(path,{},function(){  
        plus.nativeUI.closeWaiting();  
        plus.nativeUI.alert("应用资源更新完成！",function(){  
            plus.runtime.restart();  
        });  
    },function(e){  
        plus.nativeUI.closeWaiting();  
        plus.nativeUI.alert("安装wgt文件失败["+e.code+"]："+e.message);  
    });  
    }

    localStorage.setItem('navActive',0)
    window.IsSureQuit = false;
    window.noBack = false;
    mui.plusReady(() => {
      plus.key.removeEventListener('backbutton', function () { });
      plus.key.addEventListener('backbutton', function () {
        if (typeof window.noBack === "function") {
          window.noBack();
          return;
        }
        let pathName = window.location.hash;
        // tabbar路由
        let isTabbar = ["#/", "#/login", "#/myCenter"].findIndex(elem => elem === pathName);
        // 特殊路由
        let isOther = ["#/register"].findIndex(elem => elem === pathName);
        plus.nativeUI.toast(window.location.path);
        if (isTabbar > -1) {
          if (window.IsSureQuit) {
            plus.runtime.quit();
          } else {
            plus.nativeUI.toast("再按一次退出程序");
            window.IsSureQuit = true;
            setTimeout(() => {
              window.IsSureQuit = false;
            }, 2000);
          }
        } else {
          // if (history.length > 2 && isOther > -1) {
          //   window.location = "./index.html";
          // } else {
            // }
          history.back();
        }
      });
    });
  </script>
</html>
